//
//  registeredViewController.m
//  One Construction
//
//  Created by apple on 2019/9/4.
//  Copyright © 2019 apple. All rights reserved.
//

#import "registeredViewController.h"

@interface registeredViewController ()
@property(nonatomic,strong)myButton *leftBackBtn;
@property(nonatomic,strong)myLable *registLable;
@property(nonatomic,strong)myLable *loginLable;
@property(nonatomic,strong)UITextField *phoneTextField;
@property(nonatomic,strong)UITextField *passwordTextField;
@property(nonatomic,strong)UITextField *codeTextField;
@property(nonatomic,strong)UIButton *codeBtn;
@property(nonatomic,strong)UIButton *registBtn;
@end

@implementation registeredViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册";
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc]init]];
  
    [self setupView];
}
- (void)setupView{
    [self.view addSubview:self.registLable];
    //手机号的view
    UIView *phoneView = [[UIView alloc]initWithFrame:CGRectMake(28, self.registLable.frame.origin.y+self.registLable.frame.size.height+80, ScreenWidth-56, 70)];
    self.phoneTextField = [[UITextField alloc]init];
    
    [self creatView:phoneView lableText:@"手机号" textplac:@"请输入手机号" textField:self.phoneTextField];
    [self.view addSubview:phoneView];
    
    //密码的view
    UIView *passwordView = [[UIView alloc]initWithFrame:CGRectMake(28, phoneView.frame.origin.y+phoneView.frame.size.height+30, ScreenWidth-56, 70)];
    self.passwordTextField = [[UITextField alloc]init];
    
    [self creatView:passwordView lableText:@"密码" textplac:@"请输入密码" textField:self.passwordTextField];
    [self.view addSubview:passwordView];
    
    //密码的view
    UIView *codeView = [[UIView alloc]initWithFrame:CGRectMake(28, passwordView.frame.origin.y+passwordView.frame.size.height+30, ScreenWidth-56, 70)];
    self.codeTextField = [[UITextField alloc]init];
    
    [self creatView:codeView lableText:@"验证码" textplac:@"请输入验证码" textField:self.codeTextField];
    [self.view addSubview:codeView];
    [codeView addSubview:self.codeBtn];
    [self.codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(0);
        make.bottom.mas_offset(-5);
        make.width.mas_equalTo(90);
         make.height.mas_equalTo(23);
        
    }];
    [self.codeBtn addTarget:self action:@selector(clickCodeBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.registBtn];
    [self.registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(27);
        make.right.mas_offset(-27);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(codeView.mas_bottom).mas_offset(32);
        
    }];
    [self.registBtn addTarget:self action:@selector(clickRegistBtn) forControlEvents:UIControlEventTouchUpInside];
     self.passwordTextField.secureTextEntry = YES;
    
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
/*
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)creatView:(UIView *)viewcc lableText:(NSString *)labletext textplac:(NSString *)plac textField:(UITextField *)textfield {
    viewcc.backgroundColor = [UIColor whiteColor];
    
    myLable *lable = [[myLable alloc]initWithFrame:CGRectMake(10, 0, 50, 28)];
    lable.text  = labletext;
    lable.textColor = [UIColor colorWithRed:160/255.0 green:160/255.0 blue:160/255.0 alpha:1.0];
    lable.font = [UIFont systemFontOfSize:16];
    [viewcc addSubview:lable];
    
    textfield.placeholder = plac;
    textfield.textColor = [UIColor blackColor];
    [viewcc addSubview:textfield];
    textfield.frame = CGRectMake(lable.frame.origin.x, lable.frame.origin.y+lable.frame.size.height+10, 170, 25);
    
    textfield.font = [UIFont systemFontOfSize:18];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, viewcc.frame.size.height-1, viewcc.frame.size.width, 0.5)];
    lineView.backgroundColor = [UIColor colorWithRed:160/255.0 green:160/255.0 blue:160/255.0 alpha:1.0];
    [viewcc addSubview:lineView];
}
- (void)clickRegistBtn{
    
if(_phoneTextField.text.length<11||_phoneTextField.text.length>12) {
    [ProgressHUD showText:@"请输入正确的手机号格式" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
    }
else{
    if(_passwordTextField.text.length == 0) {
        [ProgressHUD showText:@"请输入密码" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
    }
    else{
        if(_codeTextField.text.length == 0) {
            [ProgressHUD showText:@"请输入验证码" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeText onView:self.view duration:1.0];
        }
        else{
            NSString *str = [UUIDManager getDeviceID];
            NSDictionary *dic = @{
                                  @"drivice_id":str,
                                  @"user_phone":self.phoneTextField.text,
                                  @"user_password":self.passwordTextField.text,
                                  @"user_code":self.codeTextField.text,
                                  @"ywls":@"",
                                  };
            
            
            [CZNetWork post:@"https://i.zhongjianedu.com/ios.php/104/user/reg2" params:dic success:^(id success) {
                NSString *isloginSTr = success[@"event"];
                if ([isloginSTr isEqualToString:@"100"]) {
                    //获取数据
                    __weak __typeof__ (self)wealSelf = self;
                    [ProgressHUD showText:@"注册成功" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeIndeterminate onView:self.view duration:1.0];
                    
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [wealSelf.navigationController popViewControllerAnimated:YES];
                    });
                }
                else{
                     [ProgressHUD showText:@"注册失败" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeIndeterminate onView:self.view duration:1.0];
                }
               
                
            } failure:^(NSError *error) {
                
            }];
        }
    }
}
}
- (void)clickCodeBtn{
    
    if (self.phoneTextField.text.length == 11) {
        NSDictionary *dic = @{
                              @"user_phone":_phoneTextField.text
                              };
        [CZNetWork post:@"https://i.zhongjianedu.com/ios.php/sms/sendsms" params:dic success:^(id success) {
            NSString *str = success[@"event"];
            if ([str isEqualToString:@"100"]) {
                [ProgressHUD showText:@"验证码已发送，请注意查收" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeIndeterminate onView:self.view duration:1.0];
            }
            else{
                  [ProgressHUD showText:@"验证码发送失败" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeIndeterminate onView:self.view duration:1.0];
                
            }
            
            
        } failure:^(NSError *error) {
            
        }];
    
    }
    else{
         [ProgressHUD showText:@"手机号格式不正确" icon:[UIImage imageNamed:@""] mode:MBProgressHUDModeIndeterminate onView:self.view duration:1.0];
    }
}

- (myLable *)registLable{
    if (!_registLable) {
        _registLable = [[myLable alloc]initWithFrame:CGRectMake(38, AppNavitionHright+44,160, 42)];
        [_registLable setLabletextColor:[UIColor blackColor] Text:@"新用户注册" TextFont:30];
        
    }
    return _registLable;
    
    
}
-(UIButton *)codeBtn{
    if (!_codeBtn) {
        _codeBtn = [[UIButton alloc]init];
        [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_codeBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [_codeBtn setTitleColor:[UIColor colorWithRed:31/255.0 green:78/255.0 blue:196/255.0 alpha:1.0] forState:UIControlStateNormal];
    }
    return _codeBtn;
}
-(UIButton *)registBtn{
    if (!_registBtn) {
        _registBtn = [[UIButton alloc]init];
        _registBtn .backgroundColor = [UIColor colorWithRed:31/255.0 green:78/255.0 blue:196/255.0 alpha:1.0];
        [_registBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
        _registBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    }

    return _registBtn;

}
@end
